const { makeInvoker } = require('awilix-express')
const { Router } = require('express')
const { responseHelper } = require('../helper')

const api = makeInvoker(
  ({
    getSystemPage,
    getSystemDetail,
    updateSystem,
    deleteSystem,
    createSystem,
  }) => {
    return {
      getSystemPage: responseHelper(async (req, res) => {
        return await getSystemPage.execute(req.query)
      }),
      getSystemDetail: responseHelper(async (req, res) => {
        return await getSystemDetail.execute(req.params.id)
      }),
      deleteSystem: responseHelper(async (req, res) => {
        return await deleteSystem.execute(req.params.id)
      }),
      createSystem: responseHelper(async (req, res) => {
        return await createSystem.execute(req.body)
      }),
      updateSystem: responseHelper(async (req, res) => {
        return await updateSystem.execute({
          id: req.params.id,
          ...req.body,
        })
      }),
    }
  }
)

module.exports = ({ all }) => {
  const router = Router()
  router
    .get('/', all, api('getSystemPage'))
    .post('/', all, api('createSystem'))
    .get('/:id', all, api('getSystemDetail'))
    .put('/:id', all, api('updateSystem'))
    .delete('/:id', all, api('deleteSystem'))
  return router
}
